<?php
class App_Module_Admin_Component_Blogs_Ctrl_Http_Web extends App_Module_Admin_Component_Blogs_Ctrl_Http
{

    protected function PrimaryActionInit ($basicActionName)
    {
        Mozg::Layout('admin')->SetMenuActiveElement('articles/blogs');
    }

    protected function GetOnlyRightBlog()
    {
		$blog = $this->GetBlogFromQueryId();
        if (!$blog)
    	{
    		Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('BLOG_NOT_FOUND'), MESSAGE_ERROR);
	       	Mozg_Router::Get('http')->Redirect(array('module' => 'admin',
		        									 'ctrl' => 'blogs',
		        									 'action' => 'bloglist'));
    	}

    	return $blog;
    }

    /**
		экшен по-умолчанию
	*/
    public function PrAction()
    {
        $this->bloglistPr();
    }

    public function bloglistPrAction()
    {
    	$this->GetBlogQueryPage();

        $t = $this->GetTpl('BlogList');
        $content = $t->GetList($this->_bpage);
        Mozg::Response('http')->SetBody($content);
    }

    public function addblogPrAction ($blog = null)
    {
        if (!$blog)
        {
        	$blog = $this->GetBlogFromQueryId();
	   		if (!$blog) $blog = Mozg_Mapper::Get('Blogs')->GetEntity();
        }

        $t = $this->GetTpl('Editblog');
        Mozg::Response('http')->SetBody($t->GetForm($blog));
    }

    public function saveblogPrAction()
    {
       	$blog = $this->GetBlogFromQueryId();
   		if (!$blog) $blog = Mozg_Mapper::Get('Blogs')->GetEntity();

   		$this->GetTpl('Editblog')->AddPostData($blog, (array)Mozg::Request('http')->getPost('blogdata'));

   		if (Mozg_Mapper::Get('Blogs')->Save($blog))
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('DATA_SAVED'));
	       	Mozg_Router::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'blogs'));
        }
        else
        {
        	if (!$blog->Validator()->GetState())
        	{
        		Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('INVALID_DATA'), MESSAGE_ERROR);
        	}
        	else
        	{
	            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('DATA_NOT_SAVED'), MESSAGE_ERROR);
        	}

        	$this->addblogPr($blog);
            return;
        }
    }

    public function delblogPrAction()
    {
       	$blog = $this->GetOnlyRightBlog();

   		if (Mozg_Mapper::Get('Blogs')->Delete(array('id' => $blog->GetId())))
   		{
			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('BLOG_DELETED'));
   		}
   		else
   		{
			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('UNABLE_TO_DELETE_BLOG'), MESSAGE_ERROR);
   		}

       	Mozg_Router::Get('http')->Redirect(array('module' => 'admin',
	        									 'ctrl' => 'blogs'));
    }

    public function listPrAction()
    {
    	$blog = $this->GetOnlyRightBlog();
		$this->OutArticles($blog);
    }

    public function addPrAction($entry = null)
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->AddArticle($blog, $entry);
    }

    public function mediaaddPrAction()
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->AddArticle($blog);
    }

    public function savePrAction ()
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->SaveArticle($blog);
    }

    public function delPrAction()
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->DelArticle($blog);
    }

    public function savethemePrAction()
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->SaveThemeArticles($blog);
    }

    public function delthemePrAction()
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->DelThemeArticle($blog);
    }

    public function commentslistPrAction()
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->OutComments($blog);
    }

    public function delcommentPrAction()
    {
    	$blog = $this->GetOnlyRightBlog();
    	$this->DelComment($blog);
    }
}